package com.ruifu.pls.service;


import com.ruifu.pls.entity.BasSku;
import com.ruifu.pls.entity.DocAsnHeader;
import com.ruifu.pls.model.skuPost.GLBasSku;
import com.ruifu.pls.model.GLCallInputParam;
import com.ruifu.pls.model.asnAddConfirm.GLReverseSkuInputRaram;
import com.ruifu.pls.repository.SkuRepository;
import com.ruifu.pls.utils.HttpUtils;
import com.ruifu.pls.utils.SighUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SkuService {

//


    @Autowired
    SkuRepository skuLogRepository;
    private org.slf4j.Logger logger = LoggerFactory.getLogger(DocAsnHeader.class);

    public void postSkuToGlink() throws ParseException {



        List<BasSku> temp1 =  skuLogRepository.findGBasSku();
      //  System.out.println(temp1==null);
        List<GLBasSku> glBasSkus = new ArrayList<GLBasSku>();

        for (int i = 0 ; i <temp1.size(); i++) {

            glBasSkus.add(temp1.get(i).gBasSku);
        }

        //具体业务接口参数模型
        //商品同步接口模型
        GLReverseSkuInputRaram glReverseSkuInputRaram = new GLReverseSkuInputRaram();
        glReverseSkuInputRaram.setType(3);
        glReverseSkuInputRaram.setItems(glBasSkus);

        // 调用际链参数模型
        GLCallInputParam glCallInputParam = new GLCallInputParam();
        glCallInputParam.setData(glReverseSkuInputRaram);//设置请求数据
        glCallInputParam.setMd5Sigh(SighUtil.getMD5(SighUtil.signParam(glCallInputParam)));
        glCallInputParam.setMethod("g2matrix.commodities.creategoods");

        logger.info(glCallInputParam.toString());
        try {
            logger.info(HttpUtils.doPost("http://demo.api.g2link.cn:8081/","router",glCallInputParam.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public   void  test()
    {
        List<BasSku> temp =  skuLogRepository. findByEdittimeBetween("2017/12/22 01:15:00","2017/12/23 01:15:00");

       //List<BasSku> temp  = skuLogRepository.getBySkuAndCustomerid("KM40FA30TI","BOXI");
    }

    public  Date getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(8);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }


}
